<template>
  <div>
    <el-form inline size="mini">
      <el-form-item label="科室">
        <el-select v-model="formData.office">
          <el-option
            v-for="item in formStatic.office"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年度">
        <el-input-number
          v-model="formData.year"
          :min="2022"
          :max="new Date().getFullYear()"
          label="年度"
        ></el-input-number>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="duQuery">Query</el-button>
        <el-button type="primary" @click="exportDocument" :disabled="!tableData"
          >Export</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as XLSX from "xlsx";
export default {
  name: "annualStatisicTable",
  data() {
    return {
      formStatic: {
        office: ["管制一室", "管制二室"],
      },
      formData: {
        year: new Date().getFullYear(),
        office: "管制二室",
      },
      tableData: null,
    };
  },
  computed: {},
  methods: {
    duQuery() {
      this.$emit("loading");
      let uri = `api/query_annual_record_progress.php?office=${this.formData.office}&year=${this.formData.year}`;
      console.log(uri);

      this.axios
        .get(uri)
        .then((response) => this.fetchTableData(response.data))
        .catch((error) => console.error(error));
    },
    fetchTableData(response) {
      // console.log(response);
      let exercises = response.exercises;
      let content = response.content;
      let output = new Array();
      for (let i = 0; i < content.length; i++) {
        let cell = {};
        cell["工号"] = content[i].number;
        cell["姓名"] = content[i].name;
        //
        cell["已做列表"] = this.plainExerciseList(content[i].list["全部已做"]);
        cell["已做数量"] = content[i].list["全部已做"].length;
        //
        cell["复训已做列表"] = this.plainExerciseList(
          content[i].list["复训已做"]
        );
        cell["复训已做数量"] = content[i].list["复训已做"].length;
        cell["复训已通过列表"] = this.plainExerciseList(
          content[i].list["复训已通过"]
        );
        cell["复训已通过数量"] = content[i].list["复训已通过"].length;
        cell["复训未通过列表"] = this.plainExerciseList(
          content[i].list["复训未通过"]
        );
        cell["复训未通过数量"] = content[i].list["复训未通过"].length;
        let notDoneList1 = this.fetchNotDone(
          exercises["科室年度复训练习列表"],
          content[i].list["复训已通过"]
        );
        cell["复训未做列表"] = this.plainExerciseList(notDoneList1);
        cell["复训未做数量"] = notDoneList1.length;
        //
        cell["其他已做列表"] = this.plainExerciseList(
          content[i].list["其他已做"]
        );
        cell["其他已做数量"] = content[i].list["其他已做"].length;
        cell["其他已通过列表"] = this.plainExerciseList(
          content[i].list["其他已通过"]
        );
        cell["其他已通过数量"] = content[i].list["其他已通过"].length;
        cell["其他未通过列表"] = this.plainExerciseList(
          content[i].list["其他未通过"]
        );
        cell["其他未通过数量"] = content[i].list["其他未通过"].length;
        let notDoneList2 = this.fetchNotDone(
          exercises["科室年度其他练习列表"],
          content[i].list["其他已通过"]
        );
        cell["其他未做列表"] = this.plainExerciseList(notDoneList2);
        cell["其他未做数量"] = notDoneList2.length;
        //
        output.push(cell);
      }
      // console.log(output);
      this.tableData = output;
      this.$emit("query", output);
      return output;
    },
    fetchNotDone(total, done) {
      if (!total || !done) {
        console.error("fetchNotDone", total, done);
        return [];
      }
      let notDoneList = new Array();
      for (let i = 0; i < total.length; i++) {
        let notDone = true;
        for (let j = 0; j < done.length; j++) {
          if (done[j].title == total[i].title) {
            notDone = false;
            continue;
          }
        }
        if (notDone) {
          notDoneList.push(total[i]);
        }
      }
      return notDoneList;
    },
    plainExerciseList(exerciseList) {
      let output = "";
      for (let i = 0; i < exerciseList.length; i++) {
        output += `(${exerciseList[i].number})${exerciseList[i].title}; `;
      }
      output = output.trimEnd("; ");
      return output;
    },
    exportDocument() {
      let fileName = "数据统计";
      // 创建工作表
      let data = XLSX.utils.json_to_sheet(this.tableData);
      // 创建工作簿
      let wb = XLSX.utils.book_new();
      // 将工作表放入工作簿中
      XLSX.utils.book_append_sheet(wb, data, "数据统计");
      // 生成文件并下载
      XLSX.writeFile(wb, fileName + ".xlsx");
      console.log(wb);
    },
  },
  mounted() {},
};
</script>
